package dk.lector.exception;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;
import java.net.UnknownHostException;

import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Looper;
import android.view.Gravity;
import android.widget.Toast;

public class ExceptionHandler implements UncaughtExceptionHandler {
	private Context appContext;
	public ExceptionHandler(Context appContext) {
		this.appContext = appContext;
	}
	public void uncaughtException(Thread arg0, Throwable t) {
		handleException(appContext, getStrackTraceAsString(t));
		t.printStackTrace();
	}
	private static void handleException(final Context appContext, final String msg) {
//		final Thread dialogThread = new Thread(new Runnable() {
//			public void run() {
//				Looper.prepare();
//				final AlertDialog alertDialog = new AlertDialog.Builder(appContext).create();
//				alertDialog.setTitle("We got an error DICKHEAD!");
//				alertDialog.setMessage(msg);
//				alertDialog.setButton("Ok, i'll fix it", new DialogInterface.OnClickListener() {
//				      public void onClick(DialogInterface dialog, int which) {
//				       alertDialog.dismiss();
//				       Thread.currentThread().interrupt();   
//				    } });
//				alertDialog.show();
//				Looper.loop();
//			}
//		});
//		dialogThread.start();
	}

	private static String getStrackTraceAsString(Throwable t) {
		final Writer result = new StringWriter();
		final PrintWriter printWriter = new PrintWriter(result);
		t.printStackTrace(printWriter);
		return result.toString();
	}
}